package com.zyfdroid.backupftpserver;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.documentfile.provider.DocumentFile;

public class LastModifiedStorageHelper extends SQLiteOpenHelper {
    private static final int version = 1;

    public LastModifiedStorageHelper(@Nullable Context context) {
        super(context, "lastmod2.db", null, version);
        db = getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        for (int i = 0; i <=version ; i++) {
            onUpgrade(db,i-1,i);
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if(newVersion == 1){
            db.execSQL("create table clm(uri text,old integer,_new integer)");
        }
    }

    private SQLiteDatabase db;

    public synchronized long getLastModifiedByUri(DocumentFile df){

            Cursor c = db.query("clm",new String[]{"old","_new"},"uri = ?",new String[]{df.getUri().toString()},null,null,null);
            long lastModified = df.lastModified();
            long old = lastModified;
            long _new = old;
            if(c.moveToFirst()){
                old = c.getLong(0);
                _new = c.getLong(1);
            }
            c.close();
            if(lastModified == old){
                return lastModified = _new;
            }
            else{
                db.delete("clm","uri = ?",new String[]{df.getUri().toString()});
            }
            return lastModified;

    }
    public synchronized long getLastModifiedByUri(String uri,long defaultTime){

        Cursor c = db.query("clm",new String[]{"old","_new"},"uri = ?",new String[]{uri},null,null,null);
        long lastModified = defaultTime;
        long old = lastModified;
        long _new = old;
        if(c.moveToFirst()){
            old = c.getLong(0);
            _new = c.getLong(1);
        }
        c.close();
        if(lastModified == old){
            return lastModified = _new;
        }
        else{
            db.delete("clm","uri = ?",new String[]{uri});
        }
        return lastModified;

    }
    public synchronized void setLastModifiedByUri(DocumentFile df,long lastModified){

            String uri = df.getUri().toString();
            db.beginTransaction();
            db.delete("clm","uri = ?",new String[]{df.getUri().toString()});
            ContentValues cv = new ContentValues();
            Log.d("LMSH", "setLastModifiedByUri: "+uri+" :: "+df.lastModified()+" -> "+lastModified);
            cv.put("uri",uri);
            cv.put("old",df.lastModified());
            cv.put("_new",lastModified);
            db.insert("clm",null,cv);
            db.setTransactionSuccessful();
            db.endTransaction();

    }
    public synchronized void deleteLastModifiedByUri(DocumentFile df){

            db.delete("clm","uri = ?",new String[]{df.getUri().toString()});

    }

    @Override
    public synchronized void close() {
        db.close();
        super.close();
    }
}
